Skip to main content

Striver A2Z DSA Sheet

A complete, step-by-step roadmap to master Data Structures & Algorithms, curated by Striver — from basics to advanced topics.

Question

Whats the differences between brute force and constructive algorithm?

The difference between brute force and constructive algorithm is mainly about how the solution is obtained.

Brute Force Approach

A brute force algorithm tries all possible possibilities until it finds the correct answer.

Idea

Exhaustively check every possible case.

Characteristics

  • Simple to implement
  • Often slow
  • Works for small inputs
  • Guarantees finding the correct answer (if implemented correctly)

Example

Find two numbers in an array that sum to k.

Brute force:

for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(a[i] + a[j] == k)
cout << i << " " << j;
}
}

Complexity:

O(n2)O(n^2)

It checks every pair.

Constructive Algorithm

A constructive algorithm builds the solution step by step using logic or patterns, without checking all possibilities.

Idea

Directly construct a valid answer.

Characteristics

  • Uses mathematical insight or problem pattern
  • Much faster
  • Common in competitive programming
  • Doesn't try every possibility

Example

Problem: Print a permutation of 1..n such that no two adjacent numbers differ by 1.

Brute force idea:

  • Generate all permutations
  • Check each one

Complexity:

O(n!)O(n!)

Constructive idea:

  • Print all even numbers first, then all odd numbers

Example for n = 5:

2 4 1 3 5

Complexity:

O(n)O(n)

Key Differences

FeatureBrute ForceConstructive
IdeaTry everythingBuild answer logically
SpeedUsually slowUsually fast
ImplementationEasyRequires insight
Typical complexity(O(n^2), O(2^n), O(n!))Often (O(n))

Competitive Programming Context

In contests like Codeforces and AtCoder:

  • Brute force is often used when constraints are small.
  • Constructive algorithms appear in problems labeled constructive or math.

Simple summary

  • Brute force: search for the answer.
  • Constructive: build the answer directly.